<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="profile-container flex flex-col gap-2">
  <mat-card appearance="outlined" class="profile-card flex flex-col">
    <mat-card-header>
      <mat-card-title>
        <span class="mat-headline-5" translate>security.security</span>
      </mat-card-title>
      <span class="flex flex-1"></span>
      <button mat-icon-button
              type="button"
              (click)="gotoSecuritySettings()"
              matTooltip="{{'security.goto-security-settings' | translate}}"
              matTooltipPosition="above">
        <mat-icon>settings</mat-icon>
      </button>
    </mat-card-header>
    <div class="change-password" tb-toast toastTarget="changePassword">
      <form #changePasswordForm="ngForm" [formGroup]="changePassword" (ngSubmit)="onChangePassword(changePasswordForm)">
        <div class="flex flex-1 flex-row gap-[25px] xs:flex-col xs:gap-0">
          <div class="flex basis-[350px] flex-col xs:basis-full">
            <h3 class="card-title" translate>profile.change-password</h3>
            <mat-form-field class="mat-block same-color" color="primary">
              <mat-label translate>profile.current-password</mat-label>
              <input matInput type="password" name="current-password" formControlName="currentPassword" autocomplete="current-password"/>
              <tb-toggle-password [class.!hidden]="!(changePassword.get('currentPassword').dirty || changePassword.get('currentPassword').touched)" matSuffix></tb-toggle-password>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.incorrect-password-try-again' | translate }}"
                        [class.!hidden]="!changePassword.get('currentPassword').hasError('differencePassword')"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
            <mat-form-field class="mat-block same-color" hideRequiredMarker color="primary" subscriptSizing="dynamic">
              <mat-label translate>login.new-password</mat-label>
              <input matInput type="password" name="new-password" formControlName="newPassword" autocomplete="new-password" required/>
              <tb-toggle-password [class.!hidden]="!(changePassword.get('newPassword').dirty || changePassword.get('newPassword').touched)" matSuffix></tb-toggle-password>
              <mat-hint></mat-hint>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-not-meet-requirements' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword').errors
                                          && !changePassword.get('newPassword').hasError('hasWhitespaces')
                                          && !changePassword.get('newPassword').hasError('samePassword')
                                          && changePassword.get('newPassword').touched)"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="changePassword.get('newPassword').getError('alreadyUsed')"
                        [class.!hidden]="!changePassword.get('newPassword').hasError('alreadyUsed')"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-should-difference' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword').hasError('samePassword') && changePassword.get('newPassword').touched)"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-should-not-contain-spaces' | translate }}"
                        [class.!hidden]="!changePassword.get('newPassword').hasError('hasWhitespaces')"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
            <div class="hidden flex-1 items-center justify-start xs:flex">
              <ng-container *ngTemplateOutlet="passwordRequirements"></ng-container>
            </div>
            <mat-form-field class="mat-block same-color" hideRequiredMarker color="primary">
              <mat-label translate>login.new-password-again</mat-label>
              <input matInput type="password" name="new-password" formControlName="newPassword2" autocomplete="new-password" required/>
              <tb-toggle-password [class.!hidden]="!(changePassword.get('newPassword2').dirty || changePassword.get('newPassword2').touched)" matSuffix></tb-toggle-password>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.new-passwords-not-match' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword2').hasError('differencePassword') && changePassword.get('newPassword2').touched)"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
          </div>
          <mat-divider [vertical]="true"></mat-divider>
          <div class="flex flex-1 xs:hidden">
            <ng-container *ngTemplateOutlet="passwordRequirements"></ng-container>
          </div>
        </div>
        <ng-template #passwordRequirements>
          @if (passwordPolicy) {
            <div class="password-requirements">
              <h3 class="card-title" translate>security.password-requirement.password-requirements</h3>
              <h4 class="mat-h4" translate>security.password-requirement.at-least</h4>
              @if (passwordPolicy.minimumUppercaseLetters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notUpperCase') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.uppercase-letter' | translate : {count: passwordPolicy.minimumUppercaseLetters} }}
                </p>
              }
              @if (passwordPolicy.minimumLowercaseLetters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notLowerCase') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.lowercase-letter' | translate : {count: passwordPolicy.minimumLowercaseLetters} }}
                </p>
              }
              @if (passwordPolicy.minimumDigits > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notNumeric') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.digit' | translate : {count: passwordPolicy.minimumDigits} }}
                </p>
              }
              @if (passwordPolicy.minimumSpecialCharacters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notSpecial') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.special-character' | translate : {count: passwordPolicy.minimumSpecialCharacters} }}
                </p>
              }
              @if (passwordPolicy.minimumLength > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('minLength') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.minimumLength} }}
                </p>
              }
              @if (passwordPolicy.maximumLength > 0) {
                <div class="password-requirements">
                  <h4 class="mat-h4" translate>security.password-requirement.at-most</h4>
                  <p class="mat-body">
                    <mat-icon class="tb-mat-20" [svgIcon]="changePassword.get('newPassword').hasError('maxLength') ? 'mdi:circle-small' : 'mdi:check'"></mat-icon>
                    {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.maximumLength} }}
                  </p>
                </div>
              }
            </div>
          }
        </ng-template>
        <div class="flex flex-row gap-2" style="margin-top: 18px;">
          <button mat-button color="primary"
            type="button"
            (click)="discardChanges(changePasswordForm, $event)"
            [disabled]="(isLoading$ | async) || !(changePassword.dirty || changePassword.touched)">
            {{ 'action.discard-changes' | translate }}
          </button>
          <button mat-raised-button color="primary"
            type="submit"
            [disabled]="(isLoading$ | async) || !(changePassword.dirty || changePassword.touched)">
            {{ 'profile.change-password' | translate }}
          </button>
        </div>
      </form>
    </div>
  </mat-card>
</div>
